package com.ruoyi.lxy.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.lxy.domain.bo.TQuestionnaireReslutBo;
import com.ruoyi.lxy.domain.vo.TQuestionnaireAnalysisVo;
import com.ruoyi.lxy.domain.vo.TQuestionnaireReslutVo;
import com.ruoyi.lxy.service.ITRouteService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lxy.domain.vo.TQuestionnaireVo;
import com.ruoyi.lxy.domain.bo.TQuestionnaireBo;
import com.ruoyi.lxy.service.ITQuestionnaireService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问卷
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lxy/questionnaire")
public class TQuestionnaireController extends BaseController {

    private final ITQuestionnaireService iTQuestionnaireService;
    private final ITRouteService iTRouteService;


    /**
     * 查询问卷列表
     */
    @SaCheckPermission("lxy:questionnaire:list")
    @GetMapping("/list")
    public TableDataInfo<TQuestionnaireVo> list(TQuestionnaireBo bo, PageQuery pageQuery) {
        return iTQuestionnaireService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出问卷列表
     */
    @SaCheckPermission("lxy:questionnaire:export")
    @Log(title = "问卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TQuestionnaireBo bo, HttpServletResponse response) {
        List<TQuestionnaireVo> list = iTQuestionnaireService.queryList(bo);
        ExcelUtil.exportExcel(list, "问卷", TQuestionnaireVo.class, response);
    }

    /**
     * 获取问卷详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("lxy:questionnaire:query")
    @GetMapping("/{id}")
    public R<TQuestionnaireVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTQuestionnaireService.queryById(id));
    }

    /**
     * 新增问卷
     */
    @SaCheckPermission("lxy:questionnaire:add")
    @Log(title = "问卷", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/{ids}")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TQuestionnaireBo bo,
                       @PathVariable Long[] ids) {
        return toAjax(iTQuestionnaireService.insertByBo(bo,ids));
    }

    /**
     * 修改问卷
     */
    @SaCheckPermission("lxy:questionnaire:edit")
    @Log(title = "问卷", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TQuestionnaireBo bo) {
        return toAjax(iTQuestionnaireService.updateByBo(bo));
    }

    /**
     * 删除问卷
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:questionnaire:remove")
    @Log(title = "问卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTQuestionnaireService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 判断该项目是否存在问卷
     */
    @SaCheckPermission("lxy:questionnaire:list")
    @PutMapping("/ifQuestionnaire/{projectId}")
    public Long ifQuestionnaire(@PathVariable Long projectId) {
        return iTQuestionnaireService.ifQuestionnaire(projectId);
    }

    /**
     * 根据某路线 统计分析结果
     */
    @SaCheckPermission("lxy:questionnaire:list")
    @GetMapping("/analysis/{projectId}/{routeId}")
    public TQuestionnaireAnalysisVo voteResultByRoute(@PathVariable("projectId")Long projectId,
                                                      @PathVariable("routeId") Long routeId) {
        System.out.println(projectId);
//        System.out.println(questionnaireId);@RequestBody Long questionnaireId,
        System.out.println(routeId);
        Long questionnaireId = null;
        questionnaireId=iTQuestionnaireService.ifQuestionnaire(projectId);

        return iTQuestionnaireService.analysis(questionnaireId,routeId);
    }

}
